package com.project.market.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class CORSFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        resp.setHeader("Access-Control-Allow-Origin", "http://localhost:9527");
        //发送请求时，请求方法允许哪些
        resp.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS,PUT,DELETE");
        //发送请求时，允许携带的头信息
        resp.setHeader("Access-Control-Allow-Headers","x-requested-with,Authorization,Content-Type,X-Cskaoyanmarket-Admin-Token,X-Cskaoyanmarket-Token");
        //发送请求时，是否允许携带Cookie凭证
        resp.setHeader("Access-Control-Allow-Credentials","true");
        filterChain.doFilter(req, resp);
    }

    @Override
    public void destroy() {

    }
}
